package cn.bdqn.service.impl;

import cn.bdqn.domain.*;
import cn.bdqn.mapper.*;
import cn.bdqn.service.DeliveryService;
import cn.bdqn.utils.result.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 派单实现类
 * @author dkj
 */
@Service
public class DeliveryServiceImpl implements DeliveryService {


    /**
     * 派单
     */
    @Autowired
    private DeliveryMapper deliveryMapper;

    /**
     * 司机
     */
    @Autowired
    private DriverMapper driverMapper;

    /**
     * 路线
     */
    @Autowired
    private RoutesMapper routesMapper;

    /**
     * 车辆
     */
    @Autowired
    private VehicleMapper vehicleMapper;

    /**
     * 发货单
     */
    @Autowired
    private SendFreightMapper sendFreightMapper;

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Delivery queryByPrimaryKey(Integer id) {
        //根据id查询
        return deliveryMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增一个派单对象
     * @param delivery
     * @return
     */
    @Override
    public Delivery saveDelivery(Delivery delivery) {

        //司机
        Driver driver = driverMapper.selectByPrimaryKey(delivery.getDriverId());
        delivery.setDriver(driver);
        //车辆
        Vehicle vehicle = vehicleMapper.selectByPrimaryKey(delivery.getVehicleId());
        delivery.setVehicle(vehicle);
        //路线
        Routes routes = routesMapper.selectByPrimaryKey(delivery.getRouteId());
        delivery.setRoute(routes);
        //发货
        SendFreight sendFreight = sendFreightMapper.selectByPrimaryKey(delivery.getSendFreightId());
        System.out.println(sendFreight.getId());
        delivery.setSendFreight(sendFreight);
        //添加
        deliveryMapper.inertDelivery(delivery);
        //返回结果
        return delivery;
    }

    /**
     * 根据订单号模糊查询 分页
     * @param currentPage 当前页
     * @param pageSize 每页显示的数量
     * @param sendFreightNumber  发货订单号
     * @return
     */
    @Override
    public Page queryBySendFreightId(Integer currentPage, Integer pageSize, String sendFreightNumber) {

        //开启分页
        PageHelper.startPage(currentPage>0?currentPage:1,pageSize);
        //根据发货单号模糊查询
        List<SendFreight> sendFreights = sendFreightMapper.selectByLikeSendFreightNumber(sendFreightNumber);

        //查询派单
        List<Delivery> deliveries = deliveryMapper.selectBySendFreightId(sendFreights);
        //封装分页数据
        PageInfo<Delivery> pageInfo = new PageInfo<>(deliveries);
        //封装分页数据
        Page page = new Page();
        //分页之后的数据
        page.setMenuList(pageInfo.getList());
        //总记录数
        page.setTotalRecord((int)pageInfo.getTotal());
        //返回
        return page;
    }



    /**
     * 更新
     * @param delivery
     */
    @Override
    public void updateDelivery(Delivery delivery) {

        //更新
        deliveryMapper.updateDelivery(delivery);
    }
}
